import _ from 'lodash';
import router from 'umi/router';
import { connect } from 'dva';

function ChoseOrg(props) {
  const { orgStructure } = props;
  const alias = props.currentUser && props.currentUser.alias ? props.currentUser.alias : '';
  const localStorageProjects = localStorage.getItem('projects');
  let projects = {};
  if (_.has(JSON.parse(localStorageProjects), alias)) {
    projects = JSON.parse(localStorageProjects)[alias];
  }
  if (!_.isEmpty(orgStructure.data)) {
    // 筛选到第一个项目
    let project = '';
    let level1Id = '';
    let level2Id = '';
    let orgnofullpath = '';
    let projectId = '';
    if (
      !_.isEmpty(projects) &&
      _.filter(orgStructure.data, { orgdepid: projects.project }).length
    ) {
      const params = projects;
      projectId = params.project;
      level1Id = params.level1;
      level2Id = params.level2;
    } else {
      project = _.filter(orgStructure.data, { hrOrgTypeId: '31' });
      orgnofullpath = project[0].orgnofullpath.split('-');
      level1Id = orgnofullpath[orgnofullpath.length - 4];
      level2Id = orgnofullpath[orgnofullpath.length - 3];
      projectId = project[0].orgdepid;
    }
    // 跳转第一个项目的全盘货值
    router.replace(`/OperatingPlan/PayBackModulus/${level1Id}/${level2Id}/${projectId}`);
  }
  return false;
}

export default connect(state => ({
  orgStructure: state.orgStructure,
  currentUser: state.user.currentUser.data,
}))(ChoseOrg);
